package com.xxgc.qinglian.qinglianserver.controller;/*
 * @Author:lsx
 * @Date:2025/6/20--06--20--20:10
 *
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxgc.qinglian.qinglianserver.entity.*;
import com.xxgc.qinglian.qinglianserver.result.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("score")
@RequiredArgsConstructor
public class ScoreController {

    @Autowired
    private ProjectEntityMapper projectEntityMapper;
    @RequestMapping("setScore")
    public Result setScore(@RequestBody Map<String, Object> params){
        String projectId = (String) params.get("projectId");
        int score = (int) params.get("finalScore");
        ProjectEntity project = new ProjectEntity();
        project.setProjectId(projectId);
        project.setScore(score);
        projectEntityMapper.updateById(project);
        return Result.success().msg("修改评分成功");
    }

    @RequestMapping("getScore")
    public Result getScore(@RequestBody Map<String, Object> params){
        String projectId = (String) params.get("projectId");
        ProjectEntity project = projectEntityMapper.selectById(projectId);
        return Result.success(project).msg("获取评分成功");
    }
    @RequestMapping("getAllScore")
    public Result getAllScore(){
        List<ProjectEntity> projects = projectEntityMapper.selectList(null);
        return Result.success(projects).msg("获取所有项目成功");
    }

    @RequestMapping("getAllVoteOnline")
    public Result getAllVoteOnline(@RequestBody Map<String, Object> params){
        String voteId = (String) params.get("voteId");
        QueryWrapper<ProjectEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("vote_id", voteId);
        List<ProjectEntity> projects = projectEntityMapper.selectList(queryWrapper);
        int voteCount = 0;
        for (ProjectEntity project : projects) {
            voteCount += project.getTotalVotes();
        }
        return Result.success(voteCount).msg("获场内总票");
    }
    @RequestMapping("getAllVote")
    public Result getAllVote(@RequestBody Map<String, Object> params){
        String voteId = (String) params.get("voteId");
        QueryWrapper<ProjectEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("vote_id", voteId);
        List<ProjectEntity> projects = projectEntityMapper.selectList(queryWrapper);
        int voteCount = 0;
        for (ProjectEntity project : projects) {
            voteCount += project.getTotalInsideVotes();
        }
        return Result.success(voteCount).msg("获场外总票");

    }
}
